package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    @RequestMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询，参数：{}", studentQueryParam);

        PageResult<Student> pageResult = studentsService.page(studentQueryParam);

        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("新增员工，员工信息：{}", student);
        studentsService.save(student);
        return Result.success();
    }


    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除员工，id：{}", ids);

        studentsService.delete(ids);

        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询员工信息，id：{}", id);

        Student student = studentsService.getById(id);

        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("编辑员工信息，员工信息：{}", student);

        studentsService.update(student);

        return Result.success();
    }


    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable short score){
        log.info("根据id更新员工违规次数和违规分值，id：{}，score：{}", id, score);

        studentsService.updateViolation(id, score);

        return Result.success();
    }
}
